package Big_O;

public class demo2 {
    // 计算bubbleSort的时间复杂度？
   public void bubbleSort(int[] array) {
        for (int end = array.length; end > 0; end--) {
            boolean sorted = true;
            for (int i = 1; i < end; i++) {
                if (array[i - 1] > array[i]) {
                    Swap(array, i - 1, i);
                    sorted = false;
                }
            }
            if (sorted == true) {
                break;
            }
        }
        for(int x:array)
        {
            System.out.print(x+" ");
        }
    }

    private void Swap(int[] array, int i, int i1) {
        int num = 0;
        num = array[i];
        array[i] = array[i1];
        array[i1] = num;
    }

    public static void main(String[] args) {
        int[] array = new int[]{2,3,5,8,7,6,9,8,1};
        demo2 d1 = new demo2();
        d1.bubbleSort(array);

    }
}
